package com.fuxi.cloud.common.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;

/**
 * GoogleBarCodeUtils
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class GoogleBarCodeUtils {
    private static final int BARCODEHEIGHT = 75;
    private static final int BARCODEWIDTH = 20;
    private static final int FRONT_COLOR = 0;
    private static final int BACKGROUND_COLOR = 16777215;

    public GoogleBarCodeUtils() {
    }

    public static byte[] createCodeToFile(String codeContent, String type, Integer width, Integer height) {
        try {
            codeContent = codeContent.trim();
            HashMap var4 = new HashMap();
            var4.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            var4.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            var4.put(EncodeHintType.MARGIN, 0);
            BitMatrix var5 = null;
            int var12;
            if (type.equals("qrcode")) {
                MultiFormatWriter var7 = new MultiFormatWriter();
                boolean var8 = false;
                if (width > height) {
                    var12 = width;
                } else {
                    var12 = height;
                }

                var5 = var7.encode(codeContent, BarcodeFormat.QR_CODE, var12, var12, var4);
                var5 = deleteWhite(var5);
                width = var5.getWidth();
                height = var5.getHeight();
            } else if (type.equals("barcode")) {
                Code128Writer var10 = new Code128Writer();
                var5 = var10.encode(codeContent, BarcodeFormat.CODE_128, width, height, var4);
            }

            BufferedImage var6 = new BufferedImage(width, height, 4);

            for(int var11 = 0; var11 < width; ++var11) {
                for(var12 = 0; var12 < height; ++var12) {
                    var6.setRGB(var11, var12, var5.get(var11, var12) ? 0 : 16777215);
                }
            }

            ByteArrayOutputStream var13 = new ByteArrayOutputStream();
            ImageIO.write(var6, "jpg", var13);
            var13.flush();
            byte[] var14 = var13.toByteArray();
            var13.close();
            return var14;
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static BufferedImage insertWords(BufferedImage image, String words, Integer width, Integer height) {
        if (StringUtils.isNotEmpty(words)) {
            BufferedImage var4 = new BufferedImage(width, 75, 1);
            Graphics2D var5 = var4.createGraphics();
            setGraphics2D(var5);
            setColorWhite(var5, width, height);
            var5.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), (ImageObserver)null);
            Color var6 = new Color(0, 0, 0);
            var5.setColor(var6);
            var5.setFont(new Font("微软雅黑", 0, 18));
            int var7 = var5.getFontMetrics().stringWidth(words);
            int var8 = (width - var7) / 2;
            int var9 = height + 20;
            var5.drawString(words, var8, var9);
            var5.dispose();
            var4.flush();
            return var4;
        } else {
            return null;
        }
    }

    private static void setGraphics2D(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        BasicStroke var1 = new BasicStroke(1.0F, 1, 0);
        g2d.setStroke(var1);
    }

    private static void setColorWhite(Graphics2D g2d, Integer width, Integer height) {
        g2d.setColor(new Color(255, 255, 255));
        g2d.fillRect(0, 0, width + 20, height + 75);
        g2d.setColor(new Color(255, 255, 255));
    }

    public static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] var1 = matrix.getEnclosingRectangle();
        int var2 = var1[2] + 1;
        int var3 = var1[3] + 1;
        BitMatrix var4 = new BitMatrix(var2, var3);
        var4.clear();

        for(int var5 = 0; var5 < var2; ++var5) {
            for(int var6 = 0; var6 < var3; ++var6) {
                if (matrix.get(var5 + var1[0], var6 + var1[1])) {
                    var4.set(var5, var6);
                }
            }
        }

        return var4;
    }

    public static void main(String[] args) {
        byte[] var1 = createCodeToFile("http://www.baidu.com", "barcode", 168, 26);

        try {
            BufferedOutputStream var2 = new BufferedOutputStream(new FileOutputStream(new File("D://images/baidu.jpg")));
            var2.write(var1);
            var2.close();
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }
}
